Import diddy
Import globals
Import udts

#rem
Since diddy can handle majority of the UI controls i will be dropping a chunk of the UI commands in the sprite editor. 



#end


Class SpriteEditState

	Field fileset:Int' = fileset
	Field spritefile:SpriteSet' = game & ".pt" & .fileset
	Field framenum:Int' = 0
	Field wide:Int' = xw
	Field high:Int' = yw
	Field perset:Int' = perset
	Field size:Int' = .wide * .high / 2
	Field setsize:Int' = .size * .perset
	Field zoom:Int' = zoom
	Field at_a_time:Int' = Int(200 / (.high + 5)) - 1
	Field fullset:Bool' = fullset
	 '--Editor
	Field x:Int' = 0
	Field y:Int' = 0
	Field zone:XYPair = New XYPair' = 0

	Field hold:Bool' = NO
	Field gotmouse:Bool' = havemouse()
	Field didscroll:Bool' = NO
	Field drawcursor:Int' = 1
	Field tool:Int' = draw_tool
	Field airsize:Int' = 5
	Field mist:Int' = 4
	Field palindex:Int' = 1
	Field psel:Int = 0
	Field previewpos:XYPair = New XYPair'.x = 319 - .wide
	
	
	
	'Field previewpos.y = 119
end



Class SpriteEd Extends Screen

	Field ss:SpriteEditState = New SpriteEditState
	Field ss_save:SpriteEditStatic
	Field menu:SimpleMenu
	Field placer:Int[]'(2 + (ss.wide * ss.high * ss.perset) \ 4) as integer
	'Don't need this since palette is global Field workpal:Palette256[]'(8 * (ss.at_a_time + 1)) as integer
	Field poffset:Int'(large(sets, ss.at_a_time)) as integer
	Field do_paste:Int = 0
	Field paste_transparent:Int = 0
	Field debug_palettes:int = 0
	Field caption:String
	'FOR Loop counters
	'should make these local
	'Field i:Int, j:Int, o:int
	Field backgroundImage:Image
	Field colorsel:Image
	Field graysel:Image
	Field state:MenuState
	Field backtoscreen:Screen
	'Field tools:Image[5]
	Field ismaptile:Bool
	Field tilset:Int
	'Field framenum:int
	'Field mapim:GameImage	
	Field info:String
	Field sets:int
	Field Pixels:Int[1]' For color picking
	'Now that i know what's what here this need to be changed
	
	'gutting the stuff i don't need
	
	Method New(xw:Int,yw:Int,fnum:Int,fileset:SpriteSet,fullset:Bool=False,zoom:Int=1,palnum:Int=0,sinfo:String="",bismaptile:Bool=False,btilset:Int=0,bto:Screen)
		ss.fileset = 0
		ss.spritefile = fileset'game & ".pt" & .fileset
		ss.framenum = fnum
		
		ss.wide = xw
		ss.high = yw
		'ss.perset = perset
		ss.size = ss.wide * ss.high / 2
		'ss.setsize = ss.size * ss.perset
		ss.zoom = zoom
		'ss.at_a_time = INT(200 / (ss.high + 5)) - 1
		ss.fullset = fullset
	 	'--Editor
		ss.x = 0
		ss.y = 0
		ss.zone.x = 0
		ss.zone.y = 0
		ss.hold = false
		ss.gotmouse = false'havemouse()
		ss.didscroll = false
		ss.drawcursor = 1
		ss.tool = 1'draw_tool
		ss.airsize = 5
		ss.mist = 4
		ss.palindex = palnum
		ss.previewpos.x = 319 - ss.wide
		ss.previewpos.y = 119
		info = sinfo
		'sets = asets
		'Local ssize:Int = 2 + (ss.wide * ss.high * ss.perset) / 4
		'Local p:Int[ssize]
		'placer = p
		'Local w:Int[8 * (ss.at_a_time + 1)]
		'workpal = Palettes
		state = New MenuState
		'state.pt = cursor_start
		'state.top = cursor_top
		'state.size = ss.at_a_time
		
		'loaddefaultpals ss.fileset, poffset(), sets <- shouldn't this be done once at the very beginning?
		'spriteedit_load_all_you_see(state.top, sets, soff)
		'setkeys
		ismaptile = bismaptile
		tilset = btilset
		backtoscreen = bto
		name = "title"
		backgroundImage = LoadImage(diddyGame.images.path + "TownExt.png")	
		colorsel = LoadImage(diddyGame.images.path + "mapsaturation.png")
		graysel = LoadImage(diddyGame.images.path + "mapgrayscale.png")
		'tools[0] = LoadImage(diddyGame.images.path + "drawtool.png")
		'tools[1] = LoadImage(diddyGame.images.path + "drawtool.png")
		'tools[2] = LoadImage(diddyGame.images.path + "drawtool.png")
		'tools[3] = LoadImage(diddyGame.images.path + "drawtool.png")
		'tools[4] = LoadImage(diddyGame.images.path + "drawtool.png")
		
		'handle rest in update loop
		
	End	






	Method spriteedit_load_all_you_see(top:Int, sets:Int, soff:int)
		For Local j:int = top To top + ss.at_a_time
	  		spriteedit_load_what_you_see(j, top, sets,soff)
		Next
	end

	Method spriteedit_load_what_you_see(j:Int,top:Int,sets:Int,soff:Int)
		Local i:Int' as integer
		'getpal256(workpal(), j - top, poffset(j)) <- need to create this function
		if j <= sets then
			'setpicstuf buffer(), ss.setsize, 2 <-- create this function
  			'loadset ss.spritefile, large(j, 0), 0 <--- create this function
 			 For i = 0 To (ss.perset - 1)
 			 	'loadsprite placer(), 0, ss.size * i, 0, ss.wide, ss.high, 2
				'stosprite placer(), 0, ss.size * i, soff * (j - top), 3
  			Next
  		Endif
	End
	
	Method Start:Void()
		menu = New SimpleMenu("ButtonOver", "ButtonClick", 0, 0, 10, False,HORIZONTAL)
		Local b:SimpleButton = menu.AddButton("drawtool.png", "drawtoolMO.png")
			b = menu.AddButton("boxtool.png", "boxtoolMO.png")
			b = menu.AddButton("linetool.png", "linetoolMO.png")
			b = menu.AddButton("filltool.png", "filltoolMO.png")
			b = menu.AddButton("replacetool.png", "replacetoolMO.png")
			b = menu.AddButton("ovaltool.png", "ovaltoolMO.png")
			b = menu.AddButton("airtool.png", "airtoolMO.png")
			b = menu.AddButton("importtool.png", "importtoolMO.png")
			
		menu.CentreHoriz()
		menu.SetY(SCREEN_HEIGHT-(b.image.h*2))
	End
	
	Method ColToRGBA:Int[](argb:Int)
			Local colors:Int[4]
			colors[0] = (argb Shr 24) & $ff
			colors[1] = (argb Shr 16) & $ff
			colors[2] = (argb Shr 8) & $ff
			colors[3] = argb & $ff	
			'Print "Colors: "+colors[0]+" "+colors[1]+" "+colors[2]
			Return colors
	End

	Method getBlue(rgb)
	 Return (rgb & $000000FF) 'Shr 16
	end
	
	Method getGreen(rgb)
	 Return (rgb & $0000FF00) Shr 8
	end
	
	Method getRed(rgb)
	 Return (rgb & $00FF0000) Shr 16
	end
	
	Method Render:Void()
		'refresh pixels
		'ss.spritefile.frames[ss.framenum].image.ReadPixelsArray()
		
		'Figure out why above is not working.
		
		Cls
		'DrawImage backgroundImage, 0, 0
		SetColor(80,80,80)
		DrawRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT)
		SetColor(255,255,255)
		DrawImage MainLogo,160,16
		Local ix:Int
		Local iy:Int	
		ix = SCREEN_WIDTH-239
		iy = (SCREEN_HEIGHT/6)+130		
		SetColor(0,0,0)
		SetAlpha(0.5)
		DrawRect(ix,iy,(16*9)+30,(16*9)+30)
		SetColor(255,255,255)
		SetAlpha(1)			
		
		DrawImage colorsel,SCREEN_WIDTH-(colorsel.Width+(SCREEN_WIDTH/6)),(SCREEN_HEIGHT/6)
		DrawImage graysel,(SCREEN_WIDTH-(colorsel.Width+(SCREEN_WIDTH/6)))+(colorsel.Width),(SCREEN_HEIGHT/6)
		
		'readpixels has to be done only during onrender :/
		ix = (SCREEN_WIDTH-(colorsel.Width+(SCREEN_WIDTH/6)))'-(colorsel.Width/2)
		iy = (SCREEN_HEIGHT/6)'-(colorsel.Height/2)
		Local mx:Int = diddyGame.mouseX
		Local my:Int = diddyGame.mouseY
		If Not diddyGame.virtualResOn
			mx = MouseX()
			my = MouseY()
		End
		If mx >= ix And mx < ix+(colorsel.Width+32) And my >= iy And my < iy+colorsel.Height Then
			If MouseDown( MOUSE_LEFT ) Then
			
				ReadPixels( Pixels, mx, my, 1, 1 )
				'set the palette color
				If ss.psel > 0 then
					Palettes[ss.palindex].col[ss.psel] = Pixels[0]
				endif
				'If ismaptile = False then
					ss.spritefile.frames[ss.framenum].Redraw(Palettes[ss.palindex])
				#rem
				Else
					Local tmpx:Int = (mx-ix)/ss.zoom
					Local tmpy:Int = (my-iy)/ss.zoom
					Local ay:Int = ((ss.framenum+1)/16)
					Local ax:Int = (ss.framenum+1)-ay
					Local finx:Int = tmpx+(ax*32)
					Local finy:Int = tmpy+(ay*32)
					ss.spritefile.frames[ss.framenum].Redraw(Palettes[ss.palindex],finx,finy)
				Endif
				#end
				'Local col:Int[] = ColToRGBA(Pixels[0])
				'Print "Got color: R: "+col[1]+" G: "+col[2]+" B: "+col[3]+" A: "+col[0]
			
			Endif
		
		Endif
		
		'pretty sure i'm gonna need to change a lot of this stuff...
		
		'draw the sprite
		ix = (SCREEN_WIDTH/10)+(ss.spritefile.frames[ss.framenum].image.w2*ss.zoom)
		iy = (SCREEN_HEIGHT/6)+(ss.spritefile.frames[ss.framenum].image.h2*ss.zoom)	
		Local iw:Int = ss.spritefile.frames[ss.framenum].image.w*ss.zoom
		Local ih:Int = ss.spritefile.frames[ss.framenum].image.h*ss.zoom
		
		SetColor(0,0,0)
		SetAlpha(0.8)
		DrawRect(ix-(ss.spritefile.frames[ss.framenum].image.w2*ss.zoom),iy-(ss.spritefile.frames[ss.framenum].image.h2*ss.zoom),iw,ih)
		SetColor(255,255,255)	
		SetAlpha(1)

		'If ismaptile = False then
		ss.spritefile.frames[ss.framenum].image.Draw(ix,iy, 0, ss.zoom, ss.zoom)
		'Else
			'TileSets[tilset].image.Draw(ix,iy, 0, ss.zoom, ss.zoom,framenum)
		'Endif
		

		ix = (SCREEN_WIDTH/10)
		iy = (SCREEN_HEIGHT/6)
		If mx >= ix And mx < ix+iw And my >= iy And my < iy+ih Then
			If MouseDown( MOUSE_LEFT ) Then
			
				'ReadPixels( Pixels, mx, my, 1, 1 )
				'Local col:Int[] = ColToRGBA(Pixels[0])
				'Print "color: "+Palettes[ss.palindex].col[ss.psel]
				Pixels[0] = Palettes[ss.palindex].col[ss.psel]

				ss.spritefile.frames[ss.framenum].image.image.WritePixels( Pixels, (mx-ix)/ss.zoom, (my-iy)/ss.zoom, 1, 1)

				ss.spritefile.frames[ss.framenum].WritePixMap((mx-ix)/ss.zoom, (my-iy)/ss.zoom,ss.psel)
				'Print "Write to sprite"
			
			Endif
		
		Endif		
		
		'Draw the Palette
		'16x16
		
		ix = SCREEN_WIDTH-224
		iy = (SCREEN_HEIGHT/6)+160
		MainFont.DrawHTML("Palette: "+ss.palindex, ix,iy-32)
		For Local yy:Int = 0 To 15
			For Local xx:Int = 0 To 15
				If ss.psel = (xx+(yy*16)) Then
					SetColor(255,255,255)
					DrawRect((ix+(xx*9))-1,(iy+(yy*9))-1,10,10)
				endif
				SetColor(getRed(Palettes[ss.palindex].col[xx+(yy*16)]),getGreen(Palettes[ss.palindex].col[xx+(yy*16)]),getBlue(Palettes[ss.palindex].col[xx+(yy*16)]))
				DrawRect(ix+(xx*9),iy+(yy*9),8,8)
				Local mx:Int = diddyGame.mouseX
				Local my:Int = diddyGame.mouseY
				If Not diddyGame.virtualResOn
					mx = MouseX()
					my = MouseY()
				Endif
				If mx >= (ix+(xx*9)) And mx < (ix+(xx*9))+8 And my >= (iy+(yy*9)) And my < (iy+(yy*9))+8 And MouseDown( MOUSE_LEFT ) Then
					ss.psel = (xx+(yy*16))
					
				endif 
			Next
		Next
		SetColor(255,255,255)

 		
		MainFont.DrawHTML(info, 20,SCREEN_HEIGHT-160)
		MainFont.DrawHTML("Tool: Draw", 20,SCREEN_HEIGHT-136)
		
		'DrawText "OHRPPGCE 2", SCREEN_WIDTH2, 10, 0.5, 0.5
	End
	
	Method ExtraRender:Void()
		menu.Draw()
	End	
	
	Method Update:Void()
		menu.Update()
		state.tog = state.tog~1
		If KeyHit(KEY_ESCAPE) Then 
			'Redraw all frames on exit.
			If ismaptile = True Then
				'oh lordy, since we are not using actual spritesets for maptiles only just taking advantage of the sprite editor
				'we need to copy the temp sprite onto the full maptil image then regenerate the tiles
				
				For Local i:Int = 0 To ss.spritefile.frames.Length-1
				
					Local stmp:HGameImage = ss.spritefile.frames[i].image
					ss.spritefile.frames[i].image = TileSets[tilset].simage
	
					'have the sprite redraw that section of the tilemap with the saved pix data using the current palette
					Local ay:Int = (i/16)
					'Print "AY: "+ay
					Local ax:Int = (i)-(ay*16)
					'Print "AX: "+ax
					Local finx:Int = (ax*32)
					Local finy:Int = (ay*32)	
					'Print "pix X: "+finx+" Y: "+finy+" Frame: "+i
					ss.spritefile.frames[i].Redraw(Palettes[ss.palindex],finx,finy)
					'set original sprite image back
					ss.spritefile.frames[i].image = stmp
				next
					'now pull new tilemap data back into the tiledimage
					Local tmp:Image = TileSets[tilset].simage.image.GrabImage( 0,0,32,32,160,0)
					Local hi:HGameImage = New HGameImage
					hi.HLoad(tmp,TileSets[tilset].name,True,True,0,0,0,False)
					TileSets[tilset].image = GameImage(hi)
					'TileSets[tilset].image.frames = 160
					'TileSets[tilset].image.w = 32
					'TileSets[tilset].image.h = 32
					TileSets[tilset].simage.tileCount = 160
					TileSets[tilset].simage.tileCountX = 16
					TileSets[tilset].simage.tileCountY = 10
					TileSets[tilset].simage.tileWidth = 32
					TileSets[tilset].simage.tileHeight = 32
					For Local i:Int = 0 To 159
						'TileSets[tilset].tiles[i] = New TileMapTile(i)
						TileSets[tilset].tiles[i].image = TileSets[tilset].simage
						TileSets[tilset].tiles[i].width = 32
						TileSets[tilset].tiles[i].height = 32
						TileSets[tilset].tiles[i].gid = i+1					
					Next					
					ss.spritefile.RedrawFrames(Palettes[ss.palindex])
					
			
			Else
				ss.spritefile.RedrawFrames(Palettes[ss.palindex])
			Endif
			If backtoscreen <> Null Then
				FadeToScreen(backtoscreen)
			else
				FadeToScreen(walkabout) 'from this point go back to the previous menu screen.
			endif
		Endif
		#rem
		 IF keyval(scF1) > 1 THEN
		  IF ss.fullset THEN
		   show_help "sprite_pickset_fullset"
		  ELSE
		   show_help "sprite_pickset"
		  END IF
		 END IF		
		#end
		
	End

End